
package com.webmarmun;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class IzmenaPorotnika extends javax.swing.JFrame {
    public String serverName = "localhost";
    public String mydatabase = "porotnici";
    public String url = "jdbc:mysql://" + serverName + "/" + mydatabase + "?useUnicode=true&characterEncoding=utf8";
    public String username = "root";
    public String password = "";
    private int suBroj;
 
    public IzmenaPorotnika() {
        initComponents();
        jLabel2.setVisible(false);
        jLabel3.setVisible(false);
        jLabel4.setVisible(false);
        jLabel5.setVisible(false);
        jLabel6.setVisible(false);
        ime.setVisible(false);
        jmbg.setVisible(false);
        adresa.setVisible(false);
        telefon.setVisible(false);
        datumAktivnosti.setVisible(false);
        jButton1.setVisible(false);
        jButton2.setVisible(false);
         try {

            java.sql.Connection connection = null;
            connection = DriverManager.getConnection(url, username, password);

            java.sql.PreparedStatement t = connection.prepareStatement("SELECT * from porotnik");
            ResultSet trs = null;
            trs = t.executeQuery();
            while (trs.next()) {
                jComboBox1.addItem(trs.getString("ime"));

            }
            
        }catch(SQLException ex){
            Logger.getLogger(Izmena.class.getName()).log(Level.SEVERE, null, ex);

        }
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        ime = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jmbg = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        adresa = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        telefon = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        datumAktivnosti = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("Измена поротника");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(28, 29, -1, -1));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Изаберите поротника" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jPanel1.add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(256, 87, 240, 34));

        jLabel2.setText("Име и презиме:");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(53, 176, -1, -1));
        jPanel1.add(ime, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 170, 350, 30));

        jLabel3.setText("ЈМБГ:");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 230, -1, -1));
        jPanel1.add(jmbg, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 230, 350, 30));

        jLabel4.setText("Адреса:");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 300, -1, -1));
        jPanel1.add(adresa, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 290, 350, 30));

        jLabel5.setText("Телефон");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 350, -1, -1));
        jPanel1.add(telefon, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 350, 350, 30));

        jLabel6.setText("Датум задње активности");
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 420, -1, -1));
        jPanel1.add(datumAktivnosti, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 410, 350, 30));

        jButton1.setBackground(new java.awt.Color(255, 51, 51));
        jButton1.setText("Одустани");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 483, -1, 40));

        jButton2.setBackground(new java.awt.Color(102, 255, 102));
        jButton2.setText("Сними");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(463, 483, 80, 40));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 707, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 533, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-776)/2, (screenSize.height-616)/2, 776, 616);
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
       jLabel2.setVisible(true);
        jLabel3.setVisible(true);
        jLabel4.setVisible(true);
        jLabel5.setVisible(true);
        jLabel6.setVisible(true);
        ime.setVisible(true);
        jmbg.setVisible(true);
        adresa.setVisible(true);
        telefon.setVisible(true);
        datumAktivnosti.setVisible(true);
        jButton1.setVisible(true);
        jButton2.setVisible(true);

         try {

            java.sql.Connection connection = null;
            connection = DriverManager.getConnection(url, username, password);

            java.sql.PreparedStatement t = connection.prepareStatement("SELECT * from porotnik where ime='"+jComboBox1.getSelectedItem().toString()+"'");
            ResultSet trs = null;
            trs = t.executeQuery();
            while (trs.next()) {
                ime.setText(trs.getString("ime"));
                jmbg.setText(trs.getString("jmbg"));
                adresa.setText(trs.getString("adresa"));
                telefon.setText(trs.getString("telefon"));
                datumAktivnosti.setText(trs.getString("datum"));
                suBroj = trs.getInt("id");

            }

        }catch(SQLException ex){
            Logger.getLogger(Izmena.class.getName()).log(Level.SEVERE, null, ex);

        }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       try {

            java.sql.Connection connection = null;
            connection = DriverManager.getConnection(url, username, password);

            java.sql.PreparedStatement t = connection.prepareStatement("UPDATE porotnik set ime=?,jmbg=?,adresa=?,telefon=?,datum=? where id="+suBroj+"");
            t.setString(1,ime.getText());
             t.setString(2,jmbg.getText());
              t.setString(3,adresa.getText());
               t.setString(4,telefon.getText());
                t.setString(5,datumAktivnosti.getText());
          
             t.executeUpdate();
             this.dispose();

        }catch(SQLException ex){
            Logger.getLogger(Izmena.class.getName()).log(Level.SEVERE, null, ex);

        }
    }//GEN-LAST:event_jButton2ActionPerformed

  
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new IzmenaPorotnika().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField adresa;
    private javax.swing.JTextField datumAktivnosti;
    private javax.swing.JTextField ime;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jmbg;
    private javax.swing.JTextField telefon;
    // End of variables declaration//GEN-END:variables

}
